package com.wolf.controller;

import com.wolf.pojo.Wl_coupon;
import com.wolf.pojo.Wl_user_admin;
import com.wolf.service.Wl_couponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class Wl_couponController {

    @Autowired
    Wl_couponService wl_couponService;

    @RequestMapping("wl_coupon_getAll")
    public String getAll(ModelMap modelMap){
        //活动状态
        List<Wl_coupon> ByStatus = wl_couponService.getByStatus();
        modelMap.addAttribute("wl_coupon_getByStatusList",ByStatus);

        List<Wl_coupon> list = wl_couponService.getAll();
        modelMap.addAttribute("wl_coupon_getAllList",list);

        //跳转查询商品分类
        return "forward:wl_coupon_getByCateName";
    }

    /**
     * 查看详情
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_coupon_getById")
    public String getById(ModelMap modelMap,String id){
        Wl_coupon wl_coupon = wl_couponService.selectByPrimaryKey(id);
        modelMap.addAttribute("wl_coupon",wl_coupon);
        return "table_Wl_coupon_Find";
    }

    /**
     * 模糊查询
     * @param modelMap
     * @param starttime
     * @param expiretime
     * @param categoryid
     * @param couponname
     * @param couponcode
     * @param status
     * @return
     */
    @RequestMapping("wl_coupon_fuzzyQuery")
    public String fuzzyQuery(ModelMap modelMap,String starttime, String expiretime, String categoryid, String couponname, String couponcode, String status){
        //活动状态
        List<Wl_coupon> ByStatus = wl_couponService.getByStatus();
        modelMap.addAttribute("wl_coupon_getByStatusList",ByStatus);

        //模糊查询
        List<Wl_coupon >list = wl_couponService.fuzzyQuery(starttime,expiretime,categoryid,couponname,couponcode,status);
        modelMap.addAttribute("wl_coupon_getAllList",list);

        return "forward:wl_coupon_getByCateName";
    }

    /**
     * 新增
     * @param wl_coupon
     * @return
     */
    @RequestMapping("wl_coupon_insert")
    public String insert(Wl_coupon wl_coupon, HttpServletRequest request){
        //设置主键的值 值为当前日期
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String now_date = df.format(new Date());
        //写入Id
        wl_coupon.setId(now_date);
        //写入优惠券编码
        now_date="YHQ"+now_date;
        wl_coupon.setCouponcode(now_date);

        //获得创建人id
        Wl_user_admin wl_user_admin=(Wl_user_admin)request.getSession().getAttribute("wl_user_admin");
        wl_coupon.setCreatebycode(wl_user_admin.getId());

        int num = wl_couponService.insertSelective(wl_coupon);
        if(num>0){
            return "redirect:wl_coupon_getAll";
        }else {
            return "500";
        }
    }

    /**
     * 修改
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_coupon_UpById")
    public String UpById(String id,ModelMap modelMap){
        Wl_coupon wl_coupon = wl_couponService.selectByPrimaryKey(id);
        modelMap.addAttribute("wl_coupon_Object",wl_coupon);
        return "wl_coupon_Update";
    }
    @RequestMapping("wl_coupon_Update")
    public String update(Wl_coupon wl_coupon,HttpServletRequest request){
        //获得修改人id
        Wl_user_admin wl_user_admin=(Wl_user_admin)request.getSession().getAttribute("wl_user_admin");
        wl_coupon.setUpdatebycode(wl_user_admin.getId());

        int num = wl_couponService.updateByPrimaryKeySelective(wl_coupon);
        if(num>0){
            return "redirect:wl_coupon_getAll";
        }else {
            return "500";
        }
    }
}
